/*
 * $Id: ShemJobGuidanceLoader.java,v 1.4 2009/07/10 20:01:40 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.processor.shemjob.guidance.loader;

import java.util.HashMap;

import org.w3c.dom.Node;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.pojo.CfgXmlParserInfo;
import com.ge.healthcare.autosc.common.exceptions.XmlParserException;
import com.ge.healthcare.autosc.common.util.ApplicationInfo;
import com.ge.healthcare.autosc.common.util.PropertyConstants;
import com.ge.healthcare.autosc.common.util.xml.dom.DomHelper;
import com.ge.healthcare.autosc.processor.exception.GuidanceLoaderException;
import com.ge.healthcare.autosc.processor.shemjob.guidance.builder.RequestDataSetBuilder;
import com.ge.healthcare.autosc.processor.shemjob.guidance.dataset.RequestDataSet;

/**
 * This loader manages ShemJob guidance
 * 
 * @author 212042946
 * 
 */
public class ShemJobGuidanceLoader extends GuidanceLoader {

	private HashMap<String, Object> guidanceMap = null;

	/**
	 * Constructor
	 * 
	 * @throws GuidanceLoaderException
	 */
	public ShemJobGuidanceLoader() throws GuidanceLoaderException {
		super();
	}

	/**
	 * For Junit test. DO NOT USE FOR NON-JUNIT TEST
	 * 
	 * @throws GuidanceLoaderException
	 */
	public void loadForTest() throws GuidanceLoaderException {
		testFlag = true;
		load();
	}

	/**
	 * Build the guidance. Since guidance is rarely changed, it is cached after
	 * it was loaded the first time.
	 */
	public void load() throws GuidanceLoaderException {
		if (ASCUtil.isNullOrEmptyMap(guidanceMap)) {
			String configFileName = ApplicationInfo.getInstance().getProperty(
					PropertyConstants.SHEMJOB_GUIDANCE_FILE,
					"ShemJobGuidance.xml");
			super.build(configFileName);
			try {
				Node sectionNode = DomHelper.getSectionNode(doc,
						"RequestDataSetList");
				if (sectionNode != null) {
					guidanceMap = buildConfigHash(sectionNode,
							"RequestDataSetList", "RequestDataSet",
							"DataSetName", "DefaultRequestDataSet",
							"DataSetGuidance", new RequestDataSetBuilder());
				}
			} catch (XmlParserException xpe) {
				throw new GuidanceLoaderException(
						"Failed building guidance from " + configFileName, xpe);
			}
		}
	}

	/**
	 * Get SHEM data set
	 * 
	 * @param key
	 *            Identifier for the data set
	 * @return RequestDataSet
	 */
	public RequestDataSet getRequestDataSet(String key) {
		RequestDataSet requestDataSet = (RequestDataSet) guidanceMap.get(key);
		if (requestDataSet == null) {
			requestDataSet = (RequestDataSet) guidanceMap.get("");
		}
		return requestDataSet;
	}

	@Override
	protected String getParserName() {
		return CfgXmlParserInfo.SHEM_JOB_GUIDANCE;
	}

}
